#! /usr/bin/python
from TOSSIM import *
import sys
import time
import random
import getopt

letters = 'n:i:t:s:'
keywords = ['noise_model=','nodes_number=','topology_id=','seed=']
#print sys.argv

opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)

#print 'Opts:',opts
#print 'Extra parameters:',extraparams

nodes = 10
model = 'low-noise.txt'
topo_id = '1'
seed = int(random.random()*1000000000)


for o,p in opts:
  if o in ['-n','--noise_model']:
     model = p
  elif o in ['-i','--nodes_number']:
     nodes = int(p)
  elif o in ['-t','--topology_id']:
     topo_id = int(p)
  elif o in ['-s','--seed']:
  	 seed = int(p)   
#print nodes+1
     


t = Tossim([])
#seed = int(random.random()*1000000000)
#seed = 999078313 
print "Random seed: ",seed
t.randomSeed(seed)

# Debug output
t.addChannel("Application",sys.stdout);
t.addChannel("Manager",sys.stdout);
t.addChannel("AdaptiveLayer",sys.stdout);
t.addChannel("LplManager",sys.stdout);
t.addChannel("Manager-Stat-Max-Ttr",sys.stdout) ;
t.addChannel("Manager-Stat-Max-Ttrd",sys.stdout) ;
#t.addChannel("REDebug",sys.stdout);
#t.addChannel("TEMP1",sys.stdout) ;
t.addChannel("Manager-Drift",sys.stdout) ;

t.addChannel("TreeRoutingCtl",sys.stdout);
t.addChannel("TreeRouting",sys.stdout);
t.addChannel("RoutingTimer",sys.stdout);
#t.addChannel("LITest",sys.stdout);
#t.addChannel("LI",sys.stdout);
t.addChannel("Forwarder",sys.stdout);
t.addChannel("Route",sys.stdout);
#t.addChannel("LE-temp",sys.stdout) ;
#t.addChannel("RMApp_state",sys.stdout);
#t.addChannel("RMApp_debug",sys.stdout);
#t.addChannel("AM",sys.stdout) ;
#t.addChannel("Acks",sys.stdout) ;
#t.addChannel("Packet",sys.stdout) ;
#t.addChannel("SNRLoss",sys.stdout) ;
#t.addChannel("DEBUG_temp1",sys.stdout) ;
#t.addChannel("CpmModelC",sys.stdout) ;
#t.addChannel("RE_BUG_1",sys.stdout);
#t.addChannel("RE_BUG_2",sys.stdout);
t.addChannel("STAT_ETX",sys.stdout) ;

# Statistics
ptsi = open('stats_ptsi.mat','w') ;
ttr = open('stats_ttr.mat','w') ;
ttrd = open('stats_ttrd.mat','w') ;
ri = open('stats_ri.mat','w') ;
gt = open('stats_gt.mat','w') ;
skip = open('stats_skip.mat','w') ;
latency = open('stats_lat.mat','w') ;
sim_topo = open('sim_topology.mat','w');
net_stat_re = open('network_stat_re.mat','w');
net_stat_fe = open('network_stat_fe.mat','w');
seed_value = open('seed.txt','w');
seed_value.write(str(nodes)+"-"+str(model)+"-"+str(topo_id)+" "+str(seed)+"\n") ;

t.addChannel("Stat_PTSI",ptsi) ;
t.addChannel("Stat_RI",ri) ;
t.addChannel("Stat_TTR",ttr) ;
t.addChannel("Stat_TTRD",ttrd) ;
t.addChannel("Stat_GT",gt) ;
t.addChannel("Stat_SKIP",skip) ;
t.addChannel("Stat_LAT",latency) ;
t.addChannel("Stat_TOPO",sim_topo) ;
t.addChannel("NetworkStatistics_RE",net_stat_re) ;
t.addChannel("NetworkStatistics_FE",net_stat_fe) ;


#throttle = Throttle(t,1)
#throttle.initialize()

topologies_dir="topologies/"
noise_dir="noise/"
seeds_dis="seeds/"

linkgain= topologies_dir+"linkgain-"+str(nodes)+"-"+str(topo_id)+".out"
noise_model=noise_dir+""+model


f = open(linkgain, "r")
r = t.radio()

lines = f.readlines()
for line in lines:
	s = line.split()
	if (len(s) > 0):
		if (s[0] == "gain"):
			r.add(int(s[1]), int(s[2]), float(s[3]))


#	s = line.split()
#	if (len(s) > 0):
#		print " ", s[0], " ", s[1], " ", s[2];
#		r.add(int(s[0]), int(s[1]), float(s[2]))

noise = open(noise_model, "r")
lines = noise.readlines()
for line in lines:
	str = line.strip()
	if (str != ""):
		val = int(str)
		for i in range(0, nodes+1):
			t.getNode(i).addNoiseTraceReading(val)

#Start sink nodes synchronized
t.getNode(0).createNoiseModel()
t.getNode(0).bootAtTime(1000)
t.getNode(1).createNoiseModel()
t.getNode(1).bootAtTime(1000)

for i in range(2, nodes+1):
	t.getNode(i).createNoiseModel()
	t.getNode(i).bootAtTime(1000+t.ticksPerSecond()*i/10)

#while(t.time() < (60*19 * t.ticksPerSecond())):
while(t.time() < (60*60*24 * t.ticksPerSecond())):
	#throttle.checkThrottle();
	t.runNextEvent();

#throttle.printStatistics()
